#include "stdafx.h"

#include <MyFC/CheckBox.h>
#include <MyFC/Utility.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

namespace My {

CheckBox::CheckBox(LPCTSTR szChecked, LPCTSTR szCheckedMask,
		LPCTSTR szUnchecked, LPCTSTR szUncheckedMask)
{
	m_Checked.Load(szChecked, szCheckedMask);
	if (strlen(szUnchecked) != 0)
		m_Unchecked.Load(szUnchecked, szUncheckedMask);
	m_Indeterminate.Load(szChecked, szCheckedMask);
}			 

CSize CheckBox::GetPreferredSize() const
{ 
	return CSize(m_Checked.Width() + 2, m_Checked.Height() + 2); 
}

void CheckBox::HandlePaint(CDC * pDC, const CRect & r)
{
	SaveObjects save(pDC);

	CRect rClient = ClientRect();
	// rClient.DeflateRect(4, 2);

	CRect rButton = m_Checked.Bounds();
	rButton.InflateRect(1, 1);
	
	rButton = AlignRect(rClient, rButton, ALIGN_VCENTER|ALIGN_HLEFT);

	if (rButton.Width() > rClient.Width() ||
		rButton.Height() > rClient.Height())
		return;

	COLORREF cL = GetSysColor(COLOR_BTNHILIGHT);
	COLORREF cS = GetSysColor(COLOR_BTNSHADOW);
	pDC->Draw3dRect(&rButton, cS, cL);

	if (!ControlEnabled(this)) {
		if (GetState() != Unchecked)
			m_Indeterminate.Draw(pDC, rButton, ALIGN_CENTER);
	} else {
		switch (GetState()) {
		case Unchecked:
			if (m_Unchecked.Size() != CSize(0, 0))
				m_Unchecked.Draw(pDC, rButton, ALIGN_CENTER);
			break;
		case Checked:
			m_Checked.Draw(pDC, rButton, ALIGN_CENTER);
			break;
		case Indeterminate:
			m_Indeterminate.Draw(pDC, rButton, ALIGN_CENTER);
			break;
		}
	}
}

} // My

